package com.xunshangwang.pinhaohuo.ui.fragments.address;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.adapters.AddressNameAdapter;
import com.xunshangwang.pinhaohuo.ui.activities.address.SelectAddressActivity;

import java.util.List;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 16/2/16.
 */
public class ProvinceFragment extends BaseAddressFragment{
    private ProvinceAdapter mAdapter;
    @Override
    public void onItemClick(BaseRecyclerAdapter adapter, View item, int position) {
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        FragmentTransaction transaction=activity.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container,CityFragment.newInstance(position),"city");
        transaction.addToBackStack(null);
        activity.getTitleText().setText(R.string.select_city);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SelectAddressActivity activity= (SelectAddressActivity) getActivity();
        mAdapter=new ProvinceAdapter(getContext(),activity.getProvinceList());
        mAdapter.setOnItemClickListener(this);

        mRecycler.setAdapter(mAdapter);
    }

    private class ProvinceAdapter extends AddressNameAdapter<SelectAddressActivity.Province> {
        public ProvinceAdapter(Context context, List<SelectAddressActivity.Province> list) {
            super(context, list);
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            NameHolder nameHolder= (NameHolder) holder;
            nameHolder.nameText.setText(getList().get(position).getName());
        }
    }
}
